package com.breeze.leetcode;

/**
 * https://leetcode-cn.com/problems/base-7/
 * 504. 七进制数
 * 给定一个整数，将其转化为7进制，并以字符串形式输出。
 */
public class LeetCode504 {
    public String convertToBase7(int num) {
        if (num == 0) return "0";
        StringBuilder builder = new StringBuilder();
        // 负数情况先按正数处理，之后追加负号即可
        boolean negative = num < 0;
        if (negative) num = -num;
        while (num > 0) {
            builder.append(num % 7);
            num /= 7;
        }
        if (negative) builder.append('-');
        return builder.reverse().toString();
    }

    /**
     * 演变形式
     * 求序列{A, B, C, ... , Z, AA, AB, AC, ... , AZ, BA, BB, ... , AAA, AAB, ...} 的第N项
     * 通过26进制处理
     */
    public String convertToBase26(int n) {
        if (n == 0) return "A";
        StringBuilder builder = new StringBuilder();
        boolean flag = false;
        while (n > 0) {
            if (!flag) {
                builder.append((char) (n % 26 + 'A'));
                flag = true;
            } else {
                builder.append((char) (n % 26 + 'A' - 1));
            }
            n /= 26;
        }
        return builder.reverse().toString();
    }

    public static void main(String[] args) {
        LeetCode504 leetCode504 = new LeetCode504();
        System.out.println(leetCode504.convertToBase7(100));
        System.out.println(leetCode504.convertToBase7(-7));
        System.out.println(leetCode504.convertToBase26(26)); // AA
        System.out.println(leetCode504.convertToBase26(0)); // A
        System.out.println(leetCode504.convertToBase26(28)); // AC
    }
}
